/* Copyright (C) 2010  Romolo Camplani, Marco Fortunato, Marco Marelli, Guido Rota, Fabio A. Schreiber et al.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package org.dei.perla.sys.persistence;
import java.sql.Array;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;


/**
 * Mappa in java il tipo sql Array, implementando l'interfaccia messa a disposizione dal 
 * pacchetto java.sql.
 * @author Luigi Rinaldi & Matteo Rovero
 */

public class ArraySql implements  Array{

	private Object vectorValue[];
	
	
	public ArraySql(Object vectorValue[]){
		this.vectorValue = vectorValue ;
	}
	
	@Override
	public void free() throws SQLException { 
		try{
			this.vectorValue = null;
		}
		catch (Exception e){
			throw new SQLException();
		}
	}

	@Override
	public Object[] getArray() throws SQLException {
		if(this.vectorValue == null) throw new SQLException();
		return vectorValue.clone();
	}

	@Override
	public Object getArray(Map<String, Class<?>> arg0) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ArraySql getArray(long index, int count) throws SQLException {	
		if(this.vectorValue == null) throw new SQLException();
		if(index-1+count > this.vectorValue.length || index < 1 || count < 1) throw new SQLException();
		Object vec[] = new Object[count];
		for(int i = (int)index-1; i < (index-1)+count; i++){
			vec[i-(int)(index-1)]= this.vectorValue[i];
		}	
		return new ArraySql(vec);
	}

	@Override
	public Object getArray(long arg0, int arg1, Map<String, Class<?>> arg2)
			throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int getBaseType() throws SQLException{
		if(this.vectorValue == null) throw new SQLException();
		if(getBaseTypeName().equals("Integer"))
			return java.sql.Types.INTEGER;
		else if(getBaseTypeName().equals("Double"))
				return java.sql.Types.DOUBLE;
			else throw new SQLException();
	}

	@Override
	public String getBaseTypeName()throws SQLException {
		if(this.vectorValue == null) throw new SQLException();
		return this.vectorValue[0].getClass().getSimpleName();
	}

	@Override
	public ResultSet getResultSet() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultSet getResultSet(Map<String, Class<?>> arg0)
			throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultSet getResultSet(long arg0, int arg1) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultSet getResultSet(long arg0, int arg1,
			Map<String, Class<?>> arg2) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}
	
	public boolean equals(Object a){
			try {
				if(this.vectorValue.length != ((ArraySql) a).getArray().length)
					return false;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		
		for(int i=0; i<this.vectorValue.length; i++)
			try {
				if(!this.vectorValue[i].equals(((ArraySql) a).getArray()[i]))
					return false;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		
		return true;
	}
}
